import com.alibaba.fastjson.{JSON, JSONObject}

import scala.io.Source

/**
 * @author: 余辉  
 * @blog: https://blog.csdn.net/silentwolfyh
 * @create: 2020-01-11 16:03
 * @description:
 **/
case class EventBean(
                      eventid: String,
                      event: Map[String, String],
                      uid: String,
                      account: String,
                      addr: String,
                      imei: String,
                      mac: String,
                      imsi: String,
                      osName: String,
                      osVer: String,
                      androidId: String,
                      resolution: String,
                      deviceType: String,
                      deviceId: String,
                      uuid: String,
                      appid: String,
                      appVer: String,
                      release_ch: String,
                      promotion_ch: String,
                      areacode: String,
                      longtitude: Double,
                      latitude: Double,
                      carrier: String,
                      netType: String,
                      cid_sn: String,
                      ip: String,
                      sessionId: String,
                      timestamp: Long,
                      var dateStr: String = null,
                      var timeStr: String = null,
                      var province: String = null,
                      var city: String = null,
                      var district: String = null
                    )

/** *
 * root
 * |-- geo: string (nullable = true)
 * |-- province: string (nullable = true)
 * |-- city: string (nullable = true)
 * |-- district: string (nullable = true)
 */

object EventBean {

  def getEventBean(line: String): EventBean = {

    try {
      val json: JSONObject = JSON.parseObject(line)

      val eventid: String = json.getString("eventid")

      val eventMap = json.getJSONObject("event")
      import scala.collection.JavaConversions._
      val event = eventMap.getInnerMap.map(tp => (tp._1, tp._2.toString)).toMap

      val user: JSONObject = json.getJSONObject("user")
      val uid = user.getString("uid")
      val account = user.getString("account")
      val addr = user.getString("addr")

      val phone: JSONObject = user.getJSONObject("phone")
      val imei = phone.getString("imei")
      val mac = phone.getString("mac")
      val imsi = phone.getString("imsi")
      val osName = phone.getString("osName")
      val osVer = phone.getString("osVer")
      val androidId = phone.getString("androidId")
      val resolution = phone.getString("resolution")
      val deviceType = phone.getString("deviceType")
      val deviceId = phone.getString("deviceId")
      val uuid = phone.getString("uuid")

      val app = user.getJSONObject("app")
      val appid = app.getString("appid")
      val appVer = app.getString("appVer")
      val release_ch = app.getString("release_ch")
      val promotion_ch = app.getString("promotion_ch")

      val loc = user.getJSONObject("loc")
      val areacode = loc.getString("areacode")
      val longtitude = loc.getDouble("longtitude")
      val latitude = loc.getDouble("latitude")
      val carrier = loc.getString("carrier")
      val netType = loc.getString("netType")
      val cid_sn = loc.getString("cid_sn")
      val ip = loc.getString("ip")

      val sessionId = user.getString("sessionId")

      val timestamp: Long = json.getLong("timestamp")

      EventBean(
        eventid,
        event,
        uid,
        account,
        addr,
        imei,
        mac,
        imsi,
        osName,
        osVer,
        androidId,
        resolution,
        deviceType,
        deviceId,
        uuid,
        appid,
        appVer,
        release_ch,
        promotion_ch,
        areacode,
        longtitude,
        latitude,
        carrier,
        netType,
        cid_sn,
        ip,
        sessionId,
        timestamp
      )
    } catch {
      case e: Exception => null
    }
  }

  def main(args: Array[String]): Unit = {
    val string: String = Source.fromFile("doc/eventLogJsonParse.json").mkString
    println("============>" + string)
    val bean: EventBean = EventBean.getEventBean(string)
    println(bean.toString)
  }
}